﻿using Baron.ToolKit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BorchePlastic
{
    //=============================================================================
    //Copyright (C) 广州中和互联网技术有限公司 
    //description ：盟立专用进制转换助手
    //author：杨金波 
    //time：2018-07-31 12:17:57      
    //=============================================================================

    /// <summary>
    /// 盟立进制转化类
    /// </summary>
    public class MLHexHelper:HexHelper
    {
        /// <summary>
        /// CRC校验
        /// </summary>
        /// <param name="data">校验数据</param>
        /// <returns>高低8位</returns>
        public static string CRC16Modbus(string data)
        {
            string[] datas = data.Split(' ');
            List<byte> bytedata = new List<byte>();

            foreach (string str in datas)
            {
                bytedata.Add(byte.Parse(str, System.Globalization.NumberStyles.AllowHexSpecifier));
            }
            byte[] crcbuf = bytedata.ToArray();
            //计算并填写CRC校验码
            int crc = 0xffff;
            int len = crcbuf.Length;
            for (int n = 0; n < len; n++)
            {
                byte i;
                crc = crc ^ crcbuf[n];
                for (i = 0; i < 8; i++)
                {
                    int TT;
                    TT = crc & 1;
                    crc = crc >> 1;
                    crc = crc & 0x7fff;
                    if (TT == 1)
                    {
                        crc = crc ^ 0xa001;
                    }
                    crc = crc & 0xffff;
                }

            }
            string[] redata = new string[2];
            redata[1] = Convert.ToString((byte)((crc >> 8) & 0xff), 16);
            redata[0] = Convert.ToString((byte)((crc & 0xff)), 16);
            // return FormatHEX(redata[0]) + " " + FormatHEX(redata[1]);
            return (redata[0].Length == 1 ? "0" + redata[0] : redata[0]) + (redata[1].Length == 1 ? "0" + redata[1] : redata[1]);
        }

        /// <summary>
        /// byte转int
        /// </summary>
        /// <param name="Hight">高位</param>
        /// <param name="Low">低位</param>
        /// <returns></returns>
        public static int byteToInt(byte Hight, byte Low)
        {
            int s = 0;   //一个16位整形变量，初值为 0000 0000 0000 0000
            byte b1 = Hight;   //一个byte的变量，作为转换后的高8位，假设初值为 0000 0001
            byte b2 = Low;   //一个byte的变量，作为转换后的低8位，假设初值为 0000 0010
            s = (int)(s ^ b1);  //将b1赋给s的低8位
            s = (int)(s << 8);  //s的低8位移动到高8位
            s = (int)(s ^ b2); //在b2赋给s的低8位
            return s;
        }


        /// <summary>
        /// 创建盟立单个命令
        /// </summary>
        /// <param name="StationNo">工作站</param>
        /// <param name="FunctionCode">函数代码</param>
        /// <param name="DataNo">取的参数个数</param>
        /// <param name="StartAddr">开始地址</param>
        /// <returns></returns>
        public static string[] CreateSendData(byte StationNo, byte FunctionCode, short DataNo, int StartAddr)
        {
            return new string[] { FunctionCode.ToString("X2") + StationNo.ToString("X2"), "0008", DataNo.ToString("X4"), StartAddr.ToString("X4") };
        }

        /// <summary>
        /// 创建盟立批量命令
        /// </summary>
        /// <param name="StationNo">工作站编号</param>
        /// <param name="FunctionCode">函数代码</param>
        /// <param name="PLCAddrs">PLC地址数组</param>
        /// <returns></returns>
        public static string[] CreateSendData(byte StationNo, byte FunctionCode, int[] PLCAddrs)
        {
            IList<string> dataList = new List<string>();
            dataList.Add(FunctionCode.ToString("X2") + StationNo.ToString("X2"));
            dataList.Add((8 + 2 * PLCAddrs.Length).ToString("X4"));
            dataList.Add(PLCAddrs.Length.ToString("X4"));
            dataList.Add("0000");
            for (int i = 0; i < PLCAddrs.Length; i++)
            {
                dataList.Add(PLCAddrs[i].ToString("X4"));
            }
            return dataList.ToArray();
        }

        /// <summary>
        /// 高低位转换
        /// </summary>
        /// <param name="str">传入数组</param>
        /// <param name="outstr">输出转换后的字符数组</param>
        /// <returns></returns>
        public static string HighLowConvert(string[] str, IList<string> outstr)
        {
            IList<string> strlist = new List<string>();
            for (int i = 0; i < str.Length; i++)
            {
                strlist.Add(str[i].Substring(2, 2) + " " + str[i].Substring(0, 2));
                outstr.Add(str[i].Substring(2, 2) + str[i].Substring(0, 2));
            }
            return string.Join(" ", strlist);
        }

        /// <summary>
        /// 高低位转换
        /// </summary>
        /// <param name="str">传入数组</param>
        /// <returns></returns>
        public static string HighLowConvert(string[] str)
        {
            IList<string> strlist = new List<string>();
            for (int i = 0; i < str.Length; i++)
            {
                strlist.Add(str[i].Substring(2, 2) + " " + str[i].Substring(0, 2));
            }
            return string.Join(" ", strlist);
        }
    }
}
